-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PYTHON-4780 Implement fast path for server selection with Primary #2416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
I didn't schedule a perf build because our perf tests only run on standalone. |
Minor note for the benchmark, thread dispatching could be dominating |
With your suggested benchmark changes: On With this PR: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work! Could you add a changelog saying "Improved performance of selecting a server with the Primary selector."?
pymongo/topology_description.py
Outdated
@@ -324,6 +324,16 @@ def apply_selector( | |||
description = self.server_descriptions().get(address) | |||
return [description] if description else [] | |||
|
|||
# Primary selection fast path. | |||
if self.topology_type == TOPOLOGY_TYPE.ReplicaSetWithPrimary and isinstance( | |||
selector, Primary |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My one concern is that technically this could be a breaking change if a user is subclassing Primary and overriding __call__
to do something special during server selection. One simple way to avoid that risk is doing type(selector) is Primary
rather than isinstance. Or we could document the potential breaking change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went with the code modification option
if self.topology_type == TOPOLOGY_TYPE.ReplicaSetWithPrimary and type(selector) is Primary: | ||
for sd in self._server_descriptions.values(): | ||
if sd.server_type == SERVER_TYPE.RSPrimary: | ||
return [sd] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh one more thing, don't we have to call the custom_selector
here if one was provided?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And add a test for it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll have to defer for now, working on a HELP ticket
Tested with the following script:
On
master
:0.4806627919897437
1.5110677920019953
With this change:
0.17988054199668113
1.233099208009662